//
//  NewProductDetController.m
//  LuCaiZhu
//
//  Created by 邓龙 on 2019/4/24.
//

#import "NewProductDetController.h"
#import "HandleNetProduct.h"
#import "myWebViewController.h"
#import "TKAlertCenter.h"
#import "CKPuslichTool.h"
#import "MyAccountCell.h"
#import "BaseInfoCell.h"
#import "WebCell.h"
#import "UserModel.h"

@interface NewProductDetController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *productTable;
@property (weak, nonatomic) IBOutlet UILabel *productName;
@property (weak, nonatomic) IBOutlet UILabel *expectedRate;
@property (weak, nonatomic) IBOutlet UILabel *bottomLab;

@property (nonatomic, copy) NSString *normalInfo;

@property (nonatomic, assign) BOOL topError;
@property (nonatomic, assign) BOOL bottomError;

@property (nonatomic, strong) BaseInfoModel *model;

@end

@implementation NewProductDetController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"产品详情";
    _productTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    _productName.text = _productModel.SubProductName;
    _expectedRate.text = _productModel.ExpectedRate;
    _bottomLab.text = [NSString stringWithFormat:@"可售规模：%@|剩余可售规模：%@%@%@",  _productModel.SumAmount, _productModel.SaleAmountShow, @"\n", _productModel.MatchingDegree];
    
    [self getBaseInfoRequest];
}

- (void)getBaseInfoRequest {
    [HandleNetProduct getBaseInfoById:_productId Success:^(NSDictionary *data) {
        _model = [BaseInfoModel mj_objectWithKeyValues:data];
        [_productTable reloadData];
    } Failure:^(NSString *errorInfo) {
        
    }];
}

-(NSString*)removeFloatAllZero:(NSString*)string
{
    NSString * testNumber = string;
    NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.floatValue)];
    return outNumber;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        if ((section == 0 && _topError) || (section == 0 && _productModel.ProductPath.length==0)) {
            return 0;
        }
        else if (section == 1) {
            return 11;
        }
        else {
            return 2;
        }
    }
    else {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 45;
    }
    else {
        if (indexPath.section == 1) {
            if (indexPath.row == 9 && [_productModel.ProductType isEqualToString:@"B"]) {
                if ([self getStrHeightAction:_model.AllocationMethod] > 45) {
                    return [self getStrHeightAction:_model.AllocationMethod]<150?[self getStrHeightAction:_model.AllocationMethod]+10:[self getStrHeightAction:_model.AllocationMethod];
                }
                else {
                    return 45;
                }
            }
            else if (indexPath.row == 10 && [_productModel.ProductType isEqualToString:@"A"]) {
                if ([self getStrHeightAction:_model.Usage] > 45) {
                    return [self getStrHeightAction:_model.Usage]<150?[self getStrHeightAction:_model.Usage]+10:[self getStrHeightAction:_model.Usage];
                }
                else {
                    return 45;
                }
            }
            else {
               return 45;
            }
        }
        else {
           return 300;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MyAccountCell *cell = nil;
        if (indexPath.section == 0 || indexPath.section == 1) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"MyAccountCell1"];
        }
        else {
            cell = [tableView dequeueReusableCellWithIdentifier:@"MyAccountCell"];
        }
        if (indexPath.section == 3) {
            cell.separatorInset = UIEdgeInsetsMake(0, self.view.frame.size.width, 0, 0);
        }
        else {
            cell.separatorInset = UIEdgeInsetsMake(0, 15, 0, 0);
        }
        switch (indexPath.section) {
            case 0:
                cell.NameLabel.text = @"产品优势";
                break;
            case 1:
                cell.NameLabel.text = @"基本信息";
                break;
            case 2:
                cell.NameLabel.text = @"个人认购提示";
                break;
            case 3:
                cell.NameLabel.text = @"机构认购提示";
                break;
        }
        return cell;
    }
    else {
        if (indexPath.section == 1) {
            BaseInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseInfoCell"];
            [cell setDataAction:_model type:_productModel.ProductType index:indexPath.row];
            return cell;
        }
        else {
            WebCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WebCell"];
            if (indexPath.section == 0) {
                [cell setViewData:_productModel.ProductPath index:indexPath.section];
            }
            else {
                [cell setViewData:_productModel.BasicPath index:indexPath.section];
            }
            
            cell.returnBlock = ^(NSInteger index) {
                if (index == 0) {
                    _topError = YES;
                }
                else {
                    _bottomError = YES;
                }
                [_productTable reloadData];
            };
            return cell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 2:
            [self getURLWith:@"Personage"];
            break;
        case 3:
            [self getURLWith:@"InstitutionalSubscription"];
            break;
    }
}

-(void)getURLWith:(NSString *)type{
    NSLog(@"self.productModel.SubProductID==%@",self.productModel.SubProductID);
    NSDictionary *params=@{@"AAAMethodName":type,
                           @"SubProductId":self.productId};
    [HandleNetProduct getDetailForParams:params Success:^(NSDictionary *data) {
        NSString *paths=data[@"Path"];
        
        myWebViewController *vc=(myWebViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"myWebViewController"];
        if ([type isEqualToString:@"Personage"]) {
            vc.title=@"个人认购提示";
        }else{
            vc.title=@"机构认购提示";
        }
        vc.urlString=paths;
        
        
        [self.navigationController pushViewController:vc animated:YES];
        
        
    } Failure:^(NSString *errorInfo) {
        
        [[TKAlertCenter defaultCenter]postAlertWithMessage:errorInfo];
        
    }];
    
}

- (float)getStrHeightAction:(NSString *)str {
    str = str?str:@"";
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:str];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 1;
    UIFont *font = [UIFont systemFontOfSize:13];
    [attributeString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, str.length)];
    [attributeString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, str.length)];
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGRect rect = [attributeString boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-175, CGFLOAT_MAX) options:options context:nil];
    return rect.size.height;
}

@end
